home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16136 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: news.nask.org.pl!usenet
  2. From: piotrpar@blue.maloka.waw.pl (Piotr Parlewicz)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: overloading ofstream constructor, help needed
  5. Date: Tue, 09 Apr 1996 18:45:44 GMT
  6. Organization: Research and Academic Computer Network
  7. Message-ID: <4keb0k$d5e@bilbo.nask.org.pl>
  8. References: <4k48qe$o9v@hobbes.cc.uga.edu>
  9. NNTP-Posting-Host: s114.maloka.waw.pl
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. greear@pollux.cs.uga.edu (Ben Greear) wrote:
  13.  
  14.  
  15. >I have a string class:  String
  16.  
  17. >I would like to be able to get an ofstream type file stream like this:
  18.  
  19. >String buf("./my_log_file");
  20.  
  21. >ofstream my_ofstream(buf);
  22.  
  23.  
  24. >I think this would entail overloading ofstream somehow, and undoubtedly
  25. >some other c++ wizardry :)
  26.  
  27.  
  28. >if anyone has an example, or advice, I would love to look at it!!
  29.  
  30. >Ben Greear                 |  "The meek shall inherit the Earth,
  31. >greear@pollux.cs.uga.edu   |   the rest of us are going to the Stars!!
  32. >Junior at UGA                        --Bumper Sticker
  33.  
  34.  
  35. Hi,
  36.  all you need to do is define a type cast operator for your class
  37. String. You will be casting to the type that ofstream requires, this
  38. is 'const char *'.
  39.  
  40. So to your class String you add the following:
  41. operator const char*() const {
  42. // your code that returns a pointer to a c type string contained in 
  43. // the String object
  44. }
  45.  
  46. Piotr Parlewicz
  47. piotrpar@blue.maloka.waw.pl
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.